package org.bing.flex.helper {
    import mx.controls.Label;
    import mx.core.Application;
    import mx.core.FlexGlobals;
    import mx.core.IFlexDisplayObject;
    import mx.core.UIComponent;
    import mx.managers.PopUpManager;

    import org.bing.flex.control.ConfirmDialog;
    import org.bing.flex.control.GeneralDialog;
    import org.bing.flex.control.SimpleDialog;

    public class PopupManager {

        public static function destory(popUp:IFlexDisplayObject):void {
            PopUpManager.removePopUp(popUp);
        }

        public static function showOnly(ui:UIComponent, title:String, width:int=600, height:int=480):void {
            var app:Application=FlexGlobals.topLevelApplication as Application;
            var dialog:GeneralDialog=PopUpManager.createPopUp(app, GeneralDialog, true) as GeneralDialog;
            dialog.toolkit=false;
            dialog.title=title;
            dialog.widget=ui;
            var targetClose:Function=null;
            if ((ui as Object).hasOwnProperty("doClose")) {
                targetClose=(ui as Object).doClose as Function;
            }
            if (targetClose != null) {
                (ui as Object).doClose=function():void {
                    targetClose();
                    dialog.doClose();
                };
            }

            PopUpManager.centerPopUp(dialog);
        }

        public static function showConfirm(msg:String, submit:Function=null, width:int=300, height:int=225):void {
            var app:Application=FlexGlobals.topLevelApplication as Application;
            var dialog:ConfirmDialog=PopUpManager.createPopUp(app, ConfirmDialog, true) as ConfirmDialog;
            dialog.submit=submit;
            var toolkit:Label=new Label();
            toolkit.text=msg;
            dialog.widget=toolkit;
            PopUpManager.centerPopUp(dialog);
        }

        public static function show(ui:UIComponent, title:String, submit:Function=null, callback:Function=null, closeOnSubmit:Boolean=false, tookit:Boolean=true, width:int=600, height:int=480):void {
            var app:Application=FlexGlobals.topLevelApplication as Application;
            var dialog:GeneralDialog=PopUpManager.createPopUp(app, GeneralDialog, true) as GeneralDialog;
            dialog.toolkit=tookit;
            dialog.submit=submit;
            dialog.callback=callback;
            dialog.closeOnSubmit=closeOnSubmit;
            dialog.widget=ui;
            dialog.width=width;
            dialog.height=height;
            dialog.title=title;
            PopUpManager.centerPopUp(dialog);
        }

        public static function simpleShow(ui:UIComponent, title:String, submit:Function=null, callback:Function=null, toolkit:Boolean=true, width:int=400, height:int=300):void {
            var app:Application=FlexGlobals.topLevelApplication as Application;
            var dialog:SimpleDialog=PopUpManager.createPopUp(app, SimpleDialog, true) as SimpleDialog;
            dialog.submit=submit;
            dialog.callback=callback;
            dialog.widget=ui;
            dialog.toolkit=toolkit;
            dialog.width=width;
            dialog.height=height;
            dialog.title=title;
            PopUpManager.centerPopUp(dialog);
        }
    }
}